/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package IU;

import Logica.Carrera;
import Logica.Fachada;
import Logica.Hipodromo;
import Logica.Jornada;
import Utilidades.ManejoFechas;
import Utilidades.Observable;
import Utilidades.Observador;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author jarzarello
 */
public class CarreraCerrar extends javax.swing.JDialog implements Observador {
    
    private final Hipodromo hs = PanelPrincipal.getInstancia().getHipodromoSeleccionado();

    /**
     * Creates new form CarreraCerrar
     */
    public CarreraCerrar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Fachada.getInstancia().agregar(this);
        cargarCarrera();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNumeroCarrera = new javax.swing.JLabel();
        lblNombreCarrera = new javax.swing.JLabel();
        lblTotalApuestas = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lblCaballosParticipantes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstCaballosParticipantes = new javax.swing.JList();
        btnCerrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblNumero = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblNumeroCarrera.setText("Número:");

        lblNombreCarrera.setText("Nombre:");

        lblTotalApuestas.setText("Total de apuestas:");

        lblCaballosParticipantes.setText("Caballos Particpantes:");

        jScrollPane1.setViewportView(lstCaballosParticipantes);

        btnCerrar.setText("Cerrar Carrera");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblTotalApuestas)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblCaballosParticipantes)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnCerrar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancelar))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(lblNombreCarrera)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(lblNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(lblNumeroCarrera)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(lblNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 176, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblNumeroCarrera, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblNumero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblNombreCarrera, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblTotalApuestas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCaballosParticipantes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCerrar)
                    .addComponent(btnCancelar))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        setBounds(0, 0, 416, 339);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        // TODO add your handling code here:
        cerrarCarrera();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCaballosParticipantes;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblNombreCarrera;
    private javax.swing.JLabel lblNumero;
    private javax.swing.JLabel lblNumeroCarrera;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblTotalApuestas;
    private javax.swing.JList lstCaballosParticipantes;
    // End of variables declaration//GEN-END:variables

    private void cerrarCarrera() {
        int opcion = JOptionPane.showConfirmDialog(null, "Realmente desea cerrar la carrera ?");
        if(opcion == JOptionPane.YES_OPTION){
            
            ManejoFechas mf = new ManejoFechas();
            Date fechaHoy = mf.formatoFecha(new Date());
        
            Jornada j = hs.buscarJornada(fechaHoy);
            Carrera c = j.buscarCarrera(Integer.parseInt(lblNumero.getText()));
        
            Fachada.getInstancia().cerrarCarrera(c);
            //c.setEstado(Carrera.cerrada);
            JOptionPane.showMessageDialog(null, "Carrera cerrada satisfactoriamente.");
            
        }
    }

    private void cargarCarrera() {
        
        ManejoFechas mf = new ManejoFechas();
        Date fechaHoy = mf.formatoFecha(new Date());
        
        Jornada j = hs.buscarJornada(fechaHoy);
        
        Carrera c = j.buscarAbierta();
        
        if(c == null){
            JOptionPane.showMessageDialog(null, "No existen carreras abiertas para esta jornada.");
            this.dispose();
        }else{
            lblNumero.setText(String.valueOf(c.getNumero()));
            lblNombre.setText(c.getNombre());
            lstCaballosParticipantes.setListData(c.getParticipantes().toArray());
        }   
    }

    @Override
    public void actualizar(Observable origen, Object param) {
        if(param.equals(Fachada.Evento.CA_CERRADA)){
            cargarCarrera();
        }
    }

}
